package com.ukao.steward.ui.login;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.bean.LoginBean;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.pesenter.logistics.PassLoginPesenter;
import com.ukao.steward.ui.main.MainActivity;
import com.ukao.steward.util.AppManager;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.view.logistics.PassLoginView;
import com.ukao.steward.widget.StateButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 密码登录
 */
public class PassLoginFragment extends MvpFragment<PassLoginPesenter> implements PassLoginView {


    @BindView(R.id.msg)
    TextView msg;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.forget_btn)
    StateButton forgetBtn;
    @BindView(R.id.signin_btn)
    Button signInBtn;
    private String mParam1;
    private String mParam2;
    private Unbinder unbinder;
    private String strPhone;

    public static PassLoginFragment newInstance(String param1, String param2) {
        PassLoginFragment fragment = new PassLoginFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1= getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    protected PassLoginPesenter createPresenter() {
        return new PassLoginPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pass_login, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }

    @Override
    protected void initView() {
        phone.setText(CheckUtils.isEmptyString(SaveParams.getAccount()));
        signInBtn.setEnabled(false);
        signInBtn.setAlpha(0.5f);
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        setStatusBarTintColor(R.color.white);

    }


    @Override
    protected void initListener() {
        super.initListener();

        phone.addTextChangedListener(useNameTextWatcher);
        password.addTextChangedListener(passTextWatcher);
        password.setOnEditorActionListener(mOnEditorActionListener);

    }

    private TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            L.i("event=" + event + "actionId=" + actionId);
            if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    || actionId == EditorInfo.IME_ACTION_DONE||actionId == EditorInfo.IME_ACTION_NEXT) {
                if (signInBtn.isEnabled()){
                    signin();
                }

            }

            return false;
        }
    };

    private MyTextWatcher useNameTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s, password);
        }
    };
    private MyTextWatcher passTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s, phone);
        }
    };

    private void isILoginGo(CharSequence s, EditText mEditText) {
        String strPass = s.toString().trim();
        String strUseName = getText(mEditText);
        if (strPass.isEmpty() || strUseName.isEmpty()) {
            if (signInBtn.isEnabled()){
                signInBtn.setAlpha(0.5f);
                signInBtn.setEnabled(false);
            }

        } else  if (!signInBtn.isEnabled()){
            signInBtn.setAlpha(1f);
            signInBtn.setEnabled(true);
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.forget_btn, R.id.signin_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.forget_btn:
              start(VerifyLoginFrament.newInstance(null,""));
                break;
            case R.id.signin_btn:
                signin();
                break;
        }
    }

    private void signin() {
        strPhone = getText(phone);
        String strpassword = getText(password);
        SaveParams.setSerialcount(strPhone);
        mvpPresenter.login(strPhone, strpassword);
    }

    @Override
    public void loadloginSucceed(LoginBean mitem) {

        /*用户信息*/
        LoginBean.WorkerInfoBean workerInfo = mitem.getWorkerInfo();
        SaveParams.setAccount(strPhone);
        SaveParams.setAlias(mitem.getAlias());
        SaveParams.saveUseName(workerInfo.getName());
        SaveParams.saveIsRest(workerInfo.getIsRest());
        SaveParams.saveToken(mitem.getAccessToken());
        SaveParams.saveHeadimgPath(workerInfo.getImgPath());
        SaveParams.saveUsePhone(workerInfo.getPhone());
        SaveParams.setIslogistics(workerInfo.getRoleId());
        SaveParams.setLogin(true);
        Intent mIntent = new Intent(_mActivity, MainActivity.class);
        startActivity(mIntent);
        AppManager.getAppManager().finishActivity(LoginActivity.class);


    }



}
